package cn.sc.allinpay.allfavour.business.common.widget;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

/**
 * @author pany
 * @description 数字键盘
 * @date 2017年3月4日下午9:46:06
 */
public class NumberKeyboardLayout extends ViewGroup implements OnClickListener {

    private Paint linePaint ;
    /**
     * 间距
     */
    public static final int divider = 4;// 菜单间距

    /**
     * 菜单尺寸
     */
    private int childSize;

    /**
     * 菜单个数
     */
    private int childCount;

    /**
     * 按钮监听
     */
    private OnKeyListener onKeyListener;

    public NumberKeyboardLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    public NumberKeyboardLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    private void init() {
        linePaint = new Paint();
        linePaint.setStrokeWidth(divider);
        linePaint.setColor(Color.parseColor("#D4D4D4"));
        setClickable(true);
        setBackgroundColor(Color.parseColor("#FAFAFA"));
    }

    @Override
    protected void onFinishInflate() {
        // TODO Auto-generated method stub
        super.onFinishInflate();
        childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            view.setOnClickListener(this);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        // 计算出所有的childView的宽和高

        childSize = measurChildSize(widthMeasureSpec);

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (i == (childCount - 1) || i == (childCount - 2)) {//最后两个
                child.measure(MeasureSpec.makeMeasureSpec(childSize,
                        MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                        childSize * 2 + 1, MeasureSpec.EXACTLY));// 两种方式都可以设置child大小
            } else {
                child.measure(MeasureSpec.makeMeasureSpec(childSize,
                        MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                        childSize, MeasureSpec.EXACTLY));// 两种方式都可以设置child大小
            }

        }
        int measurHeight = childSize * 4 + divider * 3;
        setMeasuredDimension(widthMeasureSpec, measurHeight);
    }

    /**
     * 获取菜单宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int measurChildSize(int widthMeasureSpec) {
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        return specSize / 4;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        int left = 0;
        int top = divider;
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            if (i == (childCount - 1) || i == (childCount - 2)) {
                if (i == (childCount - 2)) {
                    top = divider;
                    left = childSize * 3 + divider * 3;
                }
                view.layout(left, top, left + childSize, top + childSize * 2 + divider);
                top += top + childSize * 2 + divider;
            } else {
                view.layout(left, top, left + childSize, top + childSize);
                left = left + childSize + divider;
                if (i % 3 == 2) {
                    left = 0;
                    top += childSize + divider;
                }
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0,0,getMeasuredWidth(),0,linePaint);
    }

    private void startAnimate(final View view, final int index) {
        ValueAnimator animate = ValueAnimator.ofFloat(1, 0.95f, 1);
        animate.setDuration(200);
        animate.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // TODO Auto-generated method stub
                Float scale = (Float) animation.getAnimatedValue();
                view.setScaleX(scale);
                view.setScaleY(scale);
            }
        });
        animate.addListener(new AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
                // TODO Auto-generated method stub
                if (onKeyListener != null) {
                    onKeyListener.onKey(view, index);
                }
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                // TODO Auto-generated method stub

            }
        });
        animate.start();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startAnimate(v, indexOfChild(v));
    }

    public void setOnKeyListener(OnKeyListener onKeyListener) {
        this.onKeyListener = onKeyListener;
    }

    public interface OnKeyListener {
        void onKey(View view, int index);
    }

}
